//
//  ForgetViewModel.m
//  AppleMarket
//
//  Created by yangy on 26/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "ForgetViewModel.h"

@implementation ForgetViewModel
- (instancetype)init
{
    self = [super init];
    if(self){
        [self.refershSMSCodeCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                [SVProgressHUD showInfoWithStatus:@"发送成功"];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        } error:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
        
        [[self.refershSMSCodeCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
        
        [self.commitCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                [SVProgressHUD showInfoWithStatus:@"恭喜您，找回密码成功"];
                [SVProgressHUD dismissWithDelay:3 completion:^{
                    [self.successSubject sendNext:nil];
                }];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        } error:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
        
        [[self.commitCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
    }
    return self;
}
- (RACCommand *)refershSMSCodeCommand
{
    if(!_refershSMSCodeCommand){
        _refershSMSCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            RACTupleUnpack(NSString *key,NSString *inputCode) = input;
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                // 发送短信验证码
                [NetRequest request_SendMsgCodeType:@"ForgotLoginPwd"
                                            account:self.accountStr_
                                               code:inputCode
                                                key:key
                                    completeHandler:^(id  _Nonnull responseObj) {
                                        [subscriber sendNext:responseObj];
                                        [subscriber sendCompleted];
                                    } fail:^(NSError * _Nonnull error) {
                                        [subscriber sendError:error];
                                    }];
                return nil;
            }];
        }];
    }
    return _refershSMSCodeCommand;
}

- (RACCommand *)commitCommand
{
    if(!_commitCommand){
        _commitCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                RACTupleUnpack(NSString *key,NSString *inputCode) = input;
                [NetRequest request_forgetAccount:self.accountStr_ verifyCode:self.codeStr_ password:self.passwordStr_ imgCode:inputCode imgKey:key completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _commitCommand;
}

- (RACSubject *)sendCodeClickSubject
{
    if(!_sendCodeClickSubject){
        _sendCodeClickSubject = [RACSubject subject];
    }
    return _sendCodeClickSubject;
}

- (RACSubject *)commitClickSubject
{
    if(!_commitClickSubject){
        _commitClickSubject = [RACSubject subject];
    }
    return _commitClickSubject;
}
- (RACSubject *)successSubject
{
    if(!_successSubject){
        _successSubject = [RACSubject subject];
    }
    return _successSubject;
}
@end
